var log = console.log;
var API = API || {};



API.Booking = {
    GetData: function (url, dropdown_ID) {

        $.ajax({
            url: API.Data.base_url + 'transfers/' + url,
            dataType: 'json',
            type: "POST",
            success: function (data) {
                API.DropDown.Populate(data, dropdown_ID);
            },
            error: function (xhr, textStatus, errorThrown) {
                alert('Error ocured...\n' + xhr + '\n' + textStatus + errorThrown + '\n');
            }
        });

    },
    Init: function () {

        /***********************************************************
         * JQUERY UI Calendar
         ***********************************************************/
        API.Form.DatePicker.Init("#check_in", "#check_out", 1);

//        $('select[name=dest_from]').val("");
//        $('select[name=dest_to]').attr("disabled", "disabled");
//        API.DropDown.Populate(new Array(), "dest_to");

//        $('#return_transfer').removeAttr('checked')

        $('#transfer_form').validate({
            errorElement: "em",
            errorClass: "error",
            highlight: function (elemen, errorClass, validClass) {
                $(elemen)
                        .addClass(errorClass);
            },
            unhighlight: function (elemen, errorClass, validClass) {
                $(elemen)
                        .removeClass(errorClass);
            },
            submitHandler: function (form) {

                form.submit();

            }
        });

        $('select[name=dest_from]').on('change', function () {

            API.DropDown.Populate(new Array(), "dest_to");

            if ($(this).val() === '')
                return;

            var dropdown_ID = $(this).attr('data-id');
            var url = 'findDestTo/' + $(this).val();

            API.Booking.GetData(url, dropdown_ID);

        });

        $('#return_transfer').on('change', function () {

            if ($(this).is(":checked") == true) {
                $("#ret_transfer_controll").show();
            } else {
                $("#ret_transfer_controll").hide();
            }

        });

    }
};

API.DropDown = {
    Populate: function (items, dropdown_ID) {

        var selector = 'select[name=' + dropdown_ID + ']';

        if (items.length > 0)
            $(selector).removeAttr('disabled');

        $(selector).find('option').remove();
        $(selector).append('<option value="" >select ...</option>');

        $.each(items, function (i, item) {
            $(selector).append('<option value="' + item.ID + '" >' + item.name + '</option>');
        });

    }
};



$(function () {

    API.Booking.Init();

});